<?php

namespace App\Http\Resources\Kms\SelfCheck;

use App\Models\Cruise\CruiseCheckSelfDetailModel;
use App\Models\Cruise\CruiseTemplateModel;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class ReportListsResource extends JsonResource
{
    /**
     * Transform the resource into an array.
     *
     * @param Request $request
     * @return array
     */
    public function toArray($request): array
    {
        return [
            "id"               => $this->id,
            "title"            => $this->getTitle(),
            "sysTemplate"      => $this->sysTemplate,
            "sysTemplateText"  => CruiseTemplateModel::SYS_TEMPLATE_TEXT[$this->sysTemplate],
            "shopId"           => $this->shopId,
            "shopCode"         => attributeGet($this->shopRel, "shopCode", ""),
            "shortName"        => attributeGet($this->shopRel, "shortName", ""),
            "shopName"         => str_replace(
                ["夸父炸串", "(", ")", "（", "）"],
                "",
                attributeGet($this->shopRel, "name", "")
            ),
            "executeStartTime" => date("Y/m/d", $this->executeStartTime),
            "executeEndTime"   => date("Y/m/d", $this->executeEndTime),
            "status"           => $this->status,
            "statusText"       => CruiseCheckSelfDetailModel::NEW_STATUS_TEXT[$this->status] ?? "",
            "finishTime"       => $this->finishTime ? date("Y/m/d H:i:s", $this->finishTime) : "",
            "submitTime"       => $this->submitTime ? date("Y/m/d H:i:s", $this->submitTime) : "",
            "cruiseOrderId"    => $this->cruiseOrderId
        ];
    }

    /**
     * @return string
     */
    public function getTitle(): string
    {
        $prefix = CruiseTemplateModel::SYS_TEMPLATE_TEXT[$this->sysTemplate] ?? "";
        switch ($this->sysTemplate) {
            case CruiseTemplateModel::SYS_TEMPLATE_SELF_OPEN:
            case CruiseTemplateModel::SYS_TEMPLATE_SELF_CLOSE:
                $suffix = $this->createdAt->format("m月d日");
                break;
            case CruiseTemplateModel::SYS_TEMPLATE_SELF_WEEK:
                $suffix = vsprintf("%s-%s", [
                    $this->createdAt->startOfWeek()->format("m月d日"),
                    $this->createdAt->endOfWeek()->format("m月d日")
                ]);
                break;
            case CruiseTemplateModel::SYS_TEMPLATE_SELF_MONTH:
                $suffix = vsprintf("%s-%s", [
                    $this->createdAt->startOfMonth()->format("m月d日"),
                    $this->createdAt->endOfMonth()->format("m月d日")
                ]);
                break;
            default:
                $suffix = "";
        }
        return "$prefix($suffix)";
    }
}
